package com.guang.syncronized.review_day1;

import org.openjdk.jol.info.ClassLayout;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.TimeUnit;

/**
 * @Description
 * @Author liguang
 * @Date 2022/10/22/18:26
 */
public class SyncronziedWeightOne {
    private final static Logger logger = LoggerFactory.getLogger(SyncronziedWeightOne.class);
    static volatile boolean flag = false;
    public static void main(String[] args) throws InterruptedException {
        // 为什么让当前的锁对象为无锁可偏向
        TimeUnit.SECONDS.sleep(5);
        Object object = new Object();
        logger.info("无锁可偏向");
        Thread t1 = new Thread(() -> {
            synchronized (object) {
                while (!flag){
                    // logger.info("偏向锁，偏向t1");
                }
                logger.info("重量级锁，010。打印对象头：{}", ClassLayout.parseInstance(object).toPrintable());
            }
        }, "t1");
        t1.start();
        TimeUnit.MICROSECONDS.sleep(10);
        new Thread(()->{
            synchronized (object){
                logger.info("重量级锁，010。打印对象头：{}", ClassLayout.parseInstance(object).toPrintable());
            }
        },"t2").start();
        flag=true;
        logger.info("main执行结束");
    }
}
